import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-09-17
 * Time: 19:52
 */
public class Test {
    public static void main(String[] args) {
        int[] array={1,3,2,4,6,14,3,99,42};
        //insertSort(array);
        shellSort(array);
        System.out.println(Arrays.toString(array));
    }

        //插入排序
    public static void insertSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int key=array[i];
            int j=i-1;
            for (; j >0 ; j--){
                if(array[j+1]<array[j]){
                    array[j+1]=array[j];
                }else{
                    break;
                }
            }
            array[j+1]=key;
        }
    }


        // 希尔排序
    public static void shellSort(int[] array){
        int gap=array.length;
        while (gap>0){
            gap/=2;
            shell(array,gap);
        }
    }

    public static void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int key=array[i];
            int j=i-gap;
            for (; j >0 ; j--){
                if(array[j+gap]<array[j]){
                    array[j+gap]=array[j];
                }else{
                    break;
                }
            }
            array[j+gap]=key;
        }
    }
}





